package atri.controller;


import atri.common.Result;
import atri.mapper.RecommendMapper;
import atri.model.Book;
import atri.model.Booklist;
import atri.model.Recommend;
import atri.service.BooklistService;
import atri.service.RecommendService;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.swing.text.Document;
import java.util.ArrayList;
import java.util.List;

@RestController

public class RecommendController {


    @Autowired
    public RecommendService service;
    @Autowired
    public RecommendMapper mapper;
    @Autowired
    public BooklistService booklistService;

    @PostMapping("/recommend")
    public Result save (@RequestBody Recommend recommend){
        int a =mapper.insert(recommend);
        a = service.find_new_id();
        if(a!=0)
        {return Result.success(a);}
        else
        {return Result.fail();}
    }

    @DeleteMapping("/recommend/{id}")
    public Result remove(@PathVariable int id){
        Boolean a = service.removeById(id);
        if(a)
        {return Result.success(a);}
        else
        {return Result.fail();}
    }

//    @DeleteMapping("/recommend_under/{id}")
//    public Result remove_under_comment(@PathVariable int id){
//        System.out.println(id);
//        int a = service.delete_under_recommends(id);
//        if(a != 0)
//        {return Result.success(a);}
//        else
//        {return Result.fail();}
//    }

    @PutMapping("/recommend")
    public Result updateById(@RequestBody Recommend recommend){
        Boolean a = service.updateById(recommend);
        System.out.println(recommend);
        if(a)
        {return Result.success(a);}
        else
        {return Result.fail();}
    }

    @GetMapping("/recommends/{pages}")
    public Result Getlist(@PathVariable int pages){
        List<Recommend> s = new ArrayList<>();
        s=service.recommends_list(pages);
        if(s!=null)
        {return  Result.success(s);}
        else
        {return Result.fail();}
    }
    @GetMapping("/recommend")
    public Result Getlist(){
        List<Recommend> s = new ArrayList<>();
        int len = 0;
        len=service.list().size();
        if(s!=null)
        {return  Result.success(len);}
        else
        {return Result.fail();}
    }
    @GetMapping("/recommend/user/{id}")
    public Result Get_User_recommends(@PathVariable int id){
        List<Recommend> s = new ArrayList<>();
        s = service.recommends_user(id);
        if(s!=null)
        {return  Result.success(s);}
        else
        {return Result.fail();}
    }
    @GetMapping("/hot/recommend")
    public Result Get_hot_list(){
        List<Recommend> s = new ArrayList<>();
        s=service.recommends_hot();
        if(s!=null)
        {return  Result.success(s);}
        else
        {return Result.fail();}
    }
//
//
    @GetMapping("/recommend/{id}")
    public Result GetByid(@PathVariable int id){
        Recommend recommend = new Recommend();
        recommend = service.recommends_by_id(id);
        if(recommend != null)
        {return Result.success(recommend);}
        else
        {return Result.fail();}
    }
    @GetMapping("/recommend/s01/{content}")
    public Result search_by_content(@PathVariable("content") String content){
        List<Recommend> recommends = new ArrayList<>();
        recommends = service.recommends_content(content,1);
        if(recommends!=null)
        {return Result.success(recommends);}
        else
        {return Result.fail();}
    }

    @GetMapping("/recommend_add/{id}")
    public Result add_view(@PathVariable int id){
        Boolean recommend = false;
        recommend = service.add_view(id);
        if(recommend != null)
        {return Result.success(recommend);}
        else
        {return Result.fail();}
    }

//    @GetMapping("/recommend/category/{category}")
//    public Result search_by_category(@PathVariable("category") String category){
//        List<Recommend> recommends = new ArrayList<>();
//        recommends = service.recommends_category(category);
//        if(recommends!=null)
//        {return Result.success(recommends);}
//        else
//        {return Result.fail();}
//    }
//
//
//
//    @PostMapping("/recommend/s02")
//    public Result search_by_title(@RequestBody Recommend recommend){
//        List<Recommend> recommends = new ArrayList<>();
//        recommends = service.recommends_title(recommend.getTitle(),recommend.getId());
//        if(recommends!=null)
//        {return Result.success(recommends);}
//        else
//        {return Result.fail();}
//    }

}
